import {Ref, ref, UnwrapRef} from "vue";
import {getSmsDrivesApi, getSmsTemplateListApi} from "@/apps/admin/api/system/sms";


export type DrivesType = { drive: string, desc: string, key: string }


export type SmstmpType = { CONTENT: string, DESCRIPTION: string, KEY: string, PARAM: { key: string, value: string }[], TEMPLATE_ID: string }


/**
 * 短信驱动HOOK
 * @param auto
 */
export function useSmsDrives(auto: boolean = true) {
    const drives: Ref<UnwrapRef<DrivesType[]>> = ref([])

    /**
     * 获取短信驱动
     */
    function getSmsDrives() {
        getSmsDrivesApi().then(result => {
            if (result.code === 200) {
                let data: DrivesType[] = []
                result.data.forEach((item: any) => {
                    data.push({...item, key: item.drive.split('\\').pop()})
                })
                drives.value = data
            }
        })
    }

    if (auto) getSmsDrives()

    return {
        drives,
        getSmsDrives
    }
}


/**
 * 短信模板HOOK
 * @param auto
 */
export function useSmsTemplate(auto: boolean = true) {
    const smstmp: Ref<UnwrapRef<SmstmpType[]>> = ref([])

    const SmstmpMap: Ref<UnwrapRef<{ [key: string]: string }>> = ref({})

    function getSmsTemplateList() {
        getSmsTemplateListApi().then(result => {
            if (result.code === 200) {
                smstmp.value = result.data
                smstmp.value.forEach(item => {
                    SmstmpMap.value[item.KEY] = item.DESCRIPTION
                })
            }
        })
    }

    if (auto) getSmsTemplateList()

    return {
        getSmsTemplateList, smstmp, SmstmpMap
    }
}